"use strict";
const module$1 = require("module");
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
const require$1 = module$1.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("main.js", document.baseURI).href);
const {
  app,
  BrowserWindow,
  Menu,
  ipcMain,
  Tray,
  globalShortcut
} = require$1("electron");
const { join } = require$1("path");
const fs = require$1("fs");
const __dirname$1 = void 0;
let tray = null;
let win = null;
let settingsWin = null;
const commonWebPreferences = {
  contextIsolation: true,
  preload: join(__dirname$1, "./preload.js")
};
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
const createWindow = () => {
  if (win) return;
  win = new BrowserWindow({
    width: 600,
    height: 240,
    title: "Salary",
    frame: false,
    // 去掉默认的窗口边框，包括标题栏（应用名称，缩小放大，关闭按钮）
    resizable: false,
    // 禁止调整窗口大小
    maximizable: false,
    icon: join(__dirname$1, "../src/assets/images/logo/orange.png"),
    webPreferences: commonWebPreferences
  });
  Menu.setApplicationMenu(null);
  if (process.platform === "win32") {
    win.hookWindowMessage(278, (e) => {
      win.setEnabled(false);
      setTimeout(() => {
        win.setEnabled(true);
      }, 100);
      return true;
    });
  }
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    win.webContents.openDevTools();
  } else {
    win.loadFile(join(__dirname$1, "../dist/index.html"));
  }
};
const createSettingsWindow = () => {
  if (settingsWin) {
    settingsWin.focus();
    return;
  }
  settingsWin = new BrowserWindow({
    width: 420,
    height: 580,
    title: "设置",
    resizable: false,
    minimizable: true,
    maximizable: false,
    icon: join(__dirname$1, "../src/assets/images/logo/orange.png"),
    webPreferences: commonWebPreferences
  });
  if (process.env.VITE_DEV_SERVER_URL) {
    settingsWin.loadURL(`${process.env.VITE_DEV_SERVER_URL}#/settings`);
  } else {
    settingsWin.loadFile(join(__dirname$1, "../dist/index.html"), {
      hash: "settings"
    });
  }
  settingsWin.once("ready-to-show", () => {
    settingsWin.show();
  });
  settingsWin.on("closed", () => {
    settingsWin = null;
  });
};
app.whenReady().then(() => {
  createWindow();
  createTray();
  globalShortcut.register("Control+I", () => {
    if (BrowserWindow.getFocusedWindow()) {
      BrowserWindow.getFocusedWindow().webContents.openDevTools();
    }
  });
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
      createTray();
    }
  });
});
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
function createTray() {
  if (tray) return;
  tray = new Tray(join(__dirname$1, "../src/assets/images/logo/orange.png"));
  const contextMenu = Menu.buildFromTemplate([
    {
      label: "显示窗口",
      click: () => {
        win.show();
        win.setSkipTaskbar(false);
      }
    },
    {
      label: "设置",
      click: () => {
        if (settingsWin) {
          settingsWin.show();
          settingsWin.focus();
        } else {
          createSettingsWindow();
        }
      }
    },
    {
      label: "退出应用",
      click: () => {
        tray.destroy();
        tray = null;
        win.destroy();
        win = null;
        app.quit();
      }
    }
  ]);
  tray.setToolTip("Salary 工具");
  tray.setContextMenu(contextMenu);
  tray.on("click", () => {
    win.show();
    win.setSkipTaskbar(false);
  });
}
ipcMain.on("window-close", () => {
  win.hide();
  win.setSkipTaskbar(true);
});
ipcMain.on("setting-close", () => {
  settingsWin.hide();
});
ipcMain.handle("save-config", (event, e) => {
  const data = fs.readFileSync(join(__dirname$1, "./config.json"), "utf-8");
  const config = JSON.parse(data);
  if (e.workTime) config.workTime = e.workTime;
  if (e.offWorkTime) config.offWorkTime = e.offWorkTime;
  if (e.salary) config.salary = e.salary;
  if (e.salaryDate) config.salaryDate = e.salaryDate;
  config.holidayMode = e.holidayMode;
  if (e.themeColor) config.themeColor = e.themeColor;
  if (e.picture) config.picture = e.picture;
  fs.writeFileSync(join(__dirname$1, "./config.json"), JSON.stringify(config));
});
ipcMain.handle("load-config", () => {
  const data = fs.readFileSync(join(__dirname$1, "./config.json"), "utf-8");
  return JSON.parse(data);
});
